<?php

class Kernel_i18n_Structure extends Kernel_Filter
{
    
    /**
    * @desc Parent filter strucuter.
    * 
    * @param object
    */
    public $callback; 
    
    /**
    * @desc Supposed language. 
    * 
    */
    static private $_lang;  
    
    /**
    * @desc Languages list
    * 
    * @param array
    */
    static private $_langs = array(); 
    
    
    
    public function __construct($params = array()) 
    {
        // set language as private property
        self::$_lang = func_get_arg(1);
        
        parent::__construct($params);
    }
    
    /**
     * Start filter. 
     *
     */
    public function process() 
    {
        parent::process();
        
        if (Application::getRequest()->isMultilang()) {
            $this->_defineLanguage();
        }
        // and in the end import main lang packages
        Kernel_i18n::import('Kernel', 'Security');
    }
    
    /**
    * @desc Return languages list. 
    * 
    * @return array
    */
    static public function langs() 
    {
        return self::$_langs;
    }
    
    /**
    * Return backend interface languages list
    * 
    * @returns array
    */
    static public function getBackendInterfaceLang() 
    {
        static $cach;
        
        if (is_null($cach)) {
            // path to file where backend interface languages are located
            $PATH = SITE_ROOT . DIRECTORY_SEPARATOR . 'admin' . DIRECTORY_SEPARATOR . '_includes';
            try {
                $cach = Zend_Loader::loadFile('langs.inc.php', $PATH);
            } catch (Zend_Exception $e) { $cach = array(); }
        }
        
        return $cach;
    }
    
    /**
    * @desc Try to define application language if needed. 
    * 
    */
    private function _defineLanguage() 
    {
        // import system languages
        $this->_import();
        
        switch (true) 
        {
            case empty(self::$_langs) : 
                $this->_abort('In multilanguage system languages are not set'); 
            
            // in case of multilanguage application and absence of language in uri
            case is_null(self::$_lang) : 
                $first = current(self::$_langs);
                // relocate agent obviously indicate language
                call_user_func($this->callback, $first['code']);
            
            // in case of non-existed language announce about it obviously
            case !in_array(self::$_lang, array_values_recursive(self::$_langs)) : 
                $this->_abort('Unknown system language');
        }
        
        // after all the verifications are passed set system language 
        Kernel_i18n::lang(self::$_lang);
    }
    
    /**
    * @desc Import system languages.
    * 
    */
    private function _import() 
    {
        foreach (array('_fromFile', '_fromDB') as $func) {
            if (true === call_user_func(array($this, $func))) { break; }
        }
    }
    
    /**
    * @desc Try to load from file. Return TRUE on success otherwise FALSE. 
    * 
    * @return boolean
    */
    private function _fromFile() 
    {
        $path = SITE_ROOT . DIRECTORY_SEPARATOR . '_includes';
        try {
            self::$_langs = Zend_Loader::loadFile('langs.inc.php', $path);
        } catch (Zend_Exception $e) { return false; }
        
        return true;
    }
    
    /**
    * @desc Search in the database. Return TRUE on success otherwise FALSE. 
    * 
    * @return boolean
    */
    private function _fromDB() 
    {
        try {
            $query = '
                SELECT 
                       `id`, 
                       `code`, 
                       `name` 
                  FROM 
                       `system_langs` 
              ORDER BY 
                       `position` 
                   ASC 
                ';
            self::$_langs = database::fetchAssoc($query);
        } catch (Kernel_Db_Exception $e) { return false; }
        
        return true;
    }
    
}


/**
 * @desc Shortcut for Kernel_i18n::langs(). 
 * Return system languages. 
 *
 * @return  array
 */
function __langs() 
{
    return Kernel_i18n_Structure::langs();
}